package com.atguigu.boot.config;

import com.atguigu.boot.converter.GuiguMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: LDeng
 * @Date: 2021-08-27 12:02
 */

@Configuration
public class WebConfig {

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
               //自定义格式转换器  对象->属性值1， 属性值2，。。。
                converters.add(new GuiguMessageConverter());
            }

            @Override
            public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
                //自定义请求栏传递内容协商策略
                Map<String,MediaType> mediaTypes=new HashMap<>();
                //指定支持解析那些参数 对应的mediaType

                mediaTypes.put("xml",MediaType.APPLICATION_XML);
                mediaTypes.put("json",MediaType.APPLICATION_JSON);
                mediaTypes.put("gg",MediaType.parseMediaType("application/x-guigu"));
                ParameterContentNegotiationStrategy parameterStrategy
                        = new ParameterContentNegotiationStrategy(mediaTypes);


                HeaderContentNegotiationStrategy headerStrategy
                        = new HeaderContentNegotiationStrategy();
                configurer.strategies(Arrays.asList(headerStrategy,parameterStrategy));
            }
        };



    }






}
